0% completed
Operators in JavaScript are powerful tools that allow developers to perform various operations on values and variables. These operations can range from basic arithmetic to more complex logical comparisons. By understanding and effectively utilizing these operators, developers can implement intricate logic, manipulate data, and evaluate conditions within their applications.
JavaScript operators are categorized based on the functionality they offer, from arithmetic operations that deal with numbers to logical operations that evaluate to true or false. Each category serves a unique purpose, enabling precise control over the flow and outcome of code execution.
JavaScript Arithmetic Operators
Arithmetic operators are used to perform common mathematical operations. These operators can handle everything from addition and subtraction to more advanced calculations like modulus operations.
Table of Arithmetic Operators
| Operator | Name | Description |
|---|---|---|
+ | Addition | Adds two operands |
- | Subtraction | Subtracts second operand from the first |
* | Multiplication | Multiplies two operands |
/ | Division | Divides first operand by second |
% | Modulus | Returns remainder of division |
++ | Increment | Increases operand's value by one |
-- | Decrement | Decreases operand's value by one |
Example
In this Example:
- The addition operator (
+) addsaandb. - The subtraction operator (
-) subtractsbfroma. - The multiplication operator (
*) multipliesaandb. - The division operator (
/) dividesabyb. - The modulus operator (
%) calculates the remainder of dividingabyb. - The increment operator (
++) increasesaby one. - The decrement operator (
--) decreasesbby one.
JavaScript Comparison Operators
Comparison operators compare two values and return a Boolean value (true or false) based on whether the comparison is true.
Table of Comparison Operators
| Operator | Name | Description |
|---|---|---|
== | Equal to | True if operands are equal |
=== | Strictly equal to | True if operands are equal and of the same type |
!= | Not equal to | True if operands are not equal |
!== | Strictly not equal to | True if operands are not equal or not of the same type |
> | Greater than | True if left operand is greater than the right |
< | Less than | True if left operand is less than the right |
>= | Greater than or equal to | True if left operand is greater than or equal to the right |
<= | Less than or equal to | True if left operand is less than or equal to the right |
Example
In this Example:
x == ychecks for value equality, ignoring type, and evaluates to true because5is equal to"5".x === ychecks for value and type equality, returning false sincexis a number andyis a string.x != yandx !== ydemonstrate the not equal and strictly not equal comparisons, showing the difference when considering type.- The
>,<,>=, and<=operators compare numeric values ofxwith other numbers, determining their relational standing.
JavaScript Logical Operators
Logical operators in JavaScript are crucial for making decisions based on multiple conditions. These operators evaluate expressions to a Boolean value (true or false), allowing for complex conditional logic in applications.
Table of Logical Operators
| Operator | Name | Description |
|---|---|---|
&& | Logical AND | True if both operands are true |
|| | Logical OR | True if at least one operand is true |
! | Logical NOT | True if the operand is false |
Example
In this example:
- The logical AND (
&&) operator returns false because bothaandbare not true (bis false). - The logical OR (
\|\|) operator returns true because at least one ofaorbis true (ais true). - The logical NOT (
!) operator negates the value ofb, turning false into true.
JavaScript Bitwise Operators
Note for first-time coders: Skip the Bitwise Operators section. Jump to JavaScript Assignment Operators
Bitwise operators in JavaScript treat their operands as a set of 32 bits (zeros and ones), rather than decimal, hexadecimal, or octal numbers. These operators are used for low-level programming tasks such as graphics or cryptography.
Table of Bitwise Operators
| Operator | Name | Description |
|---|---|---|
& | Bitwise AND | Each bit of the result is 1 if both bits are 1 |
| | Bitwise OR | Each bit of the result is 1 if at least one bit is 1 |
^ | Bitwise XOR | Each bit of the result is 1 if only one of the bits is 1 |
~ | Bitwise NOT | Inverts all the bits |
<< | Left shift | Shifts left by pushing zeros in from the right and let the leftmost bits fall off |
>> | Right shift | Shifts right by pushing copies of the leftmost bit in from the left, and let the rightmost bits fall off |
>>> | Zero-fill right shift | Shifts right by pushing zeros in from the left, and let the rightmost bits fall off |
Example
In this example:
- Bitwise AND (
&) returns 0 because there are no positions where bothcanddhave a 1. - Bitwise OR (
\|) returns 7 (111 in binary) because it combines the bits ofcandd. - Bitwise XOR (
^) also returns 7, as it reflects the positions wherecandddiffer. - Bitwise NOT (
~) inverts the bits ofd, resulting in-3. - Left shift (
<<) moves the bits ofcone position to the left, doubling its value. - Right shift (
>>) and Zero-fill right shift (>>>) move the bits ofcone position to the right, halving its value, but differ in handling the leftmost bits.
JavaScript Assignment Operators
Assignment operators in JavaScript are used to assign values to variables. The simple assignment operator (=) assigns the right operand's value to the left operand. Other assignment operators modify the variable's value in place according to the operation performed before the assignment.
Note for first-time coders: You may skip the details of any bitwise assignment operators.
Table of Assignment Operators
| Operator | Name | Description |
|---|---|---|
= | Assignment | x = y |
+= | Addition assignment | x = x + y |
-= | Subtraction assignment | x = x - y |
*= | Multiplication assignment | x = x * y |
/= | Division assignment | x = x / y |
%= | Modulus assignment | x = x % y |
<<= | Left shift assignment | x = x << y |
>>= | Right shift assignment | x = x >> y |
&= | Bitwise AND assignment | x = x & y |
^= | Bitwise XOR assignment | x = x ^ y |
|= | Bitwise OR assignment | x = x | y |
Example
In this example:
- Each assignment operator modifies
ein place, starting withe = 10and then applying various operations like addition (+=), subtraction (-=), multiplication (*=), and so on. - The result of each operation is immediately assigned back to
e, demonstrating how assignment operators can simplify in-place modifications of variable values.
Operators in JavaScript are fundamental tools that allow for the manipulation of values, enabling arithmetic calculations, value comparisons, logical operations, and much more. Through this exploration of various types of operators, from arithmetic to assignment, we've seen how they can be applied to control the flow of data and logic within our programs.
Understanding and mastering the use of these operators is essential for any JavaScript developer aiming to build dynamic, efficient, and sophisticated applications. By effectively leveraging operators, developers can implement complex logic with precision and ease, making JavaScript a powerful language for web development.
On this page
JavaScript Arithmetic Operators
Table of Arithmetic Operators
Example
JavaScript Comparison Operators
Table of Comparison Operators
Example
JavaScript Logical Operators
Table of Logical Operators
Example
JavaScript Bitwise Operators
Table of Bitwise Operators
Example
JavaScript Assignment Operators
Table of Assignment Operators
Example